package com.vcredit.bird.service.foreignTradeCompliance;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.vcredit.bird.service.base.BirdResponse;
import com.vcredit.bird.service.base.BirdService;
import com.vcredit.bird.vo.foreignTradeCompliance.ForeignSubmitParam;
import com.vcredit.bird.vo.foreignTradeCompliance.PreAuditParam;
import com.vcredit.bird.vo.foreignTradeCompliance.PreAuditResponse;
import com.vcredit.bird.vo.foreignTradeCompliance.AssureSubmitParam;
import com.vcredit.bird.vo.foreignTradeCompliance.AssureSubmitResponse;
import com.vcredit.bird.vo.foreignTradeCompliance.ComplianceApplyResponse;
import com.vcredit.bird.vo.foreignTradeCompliance.ComplianceParam;
import com.vcredit.bird.vo.foreignTradeCompliance.ComplianceQueryParam;
import com.vcredit.bird.vo.foreignTradeCompliance.ComplianceQueryResponse;
import com.vcredit.bird.vo.foreignTradeCompliance.ReportStateResponse;
import com.vcredit.creditcat.enumtype.ProductBusTypeEnum;
import com.vcredit.creditcat.exception.BirdRequestException;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.util.HttpRequest;
import com.vcredit.framework.util.JacksonUtil;

/**
 * 机构版征信 合规外贸征信接口
 * 
 * @author zhangboshi
 *
 */
@Service
public class ForeignTradeComplianceService extends BirdService {

  private Logger logger = LoggerFactory.getLogger(ForeignTradeComplianceService.class);

  /*
   * 外贸征信开关，为1时使用外贸征信，为0时使用担保征信
   */
  /*@Value("${foreignCreditOpenFlag}")
  private int foreignCreditOpenFlag;*/
  /**
   * 外贸征信接口url
   */
  @Value("${foreignTrade.compliance.creditApply}")
  private String foreignApplyUrl;
  @Value("${foreignTrade.compliance.query}")
  private String foreignQueryUrl;
  @Value("${foreignTrade.report.state}")
  private String foreignStateUrl;
  @Value("${foreignTrade.report.result}")
  private String foreignResultUrl;
  @Value("${foreignTrade.report.submit}")
  private String foreignSubmitUrl;
  
  /**
   * 担保征信接口url
   */
  @Value("${assure.compliance.apply}")
  private String assureApplyUrl;
  @Value("${assure.compliance.query}")
  private String assureQueryUrl;
  @Value("${assure.report.state}")
  private String assureStateUrl;
  @Value("${assure.report.result}")
  private String assureResultUrl;
  @Value("${assure.report.submit}")
  private String assureSubmitUrl;
  
  @Value("${preAudit.url}")
  private String preAuditUrl;

  /**
   * 合规外贸征信申请接口
   * 
   * @param applyInfo
   * @throws Exception
   * @return ComplianceCreditApplyVo
   */
  public ComplianceApplyResponse creditApply(ComplianceParam applyInfo,Long productId) throws Exception {    
    try {    
      int foreignCreditOpenFlag = getCreditChoice(productId);
      String applyUrl ="";
      if(foreignCreditOpenFlag==1){
    	  applyUrl = foreignApplyUrl;
      }else{
    	  applyUrl = assureApplyUrl;
      }
      String paramData = JacksonUtil.Obj2Json(applyInfo);
      String result = HttpRequest.doJson(applyUrl, paramData, 30);
      ComplianceApplyResponse response =
          JacksonUtil.Json2Obj(result,
              new TypeReference<ComplianceApplyResponse>() {});      
      return response;
    } catch (Exception e) {
      throw new BirdRequestException(e.getMessage());
    }
  }

  /**
   * 合规查询接口
   * 
   * @param queryParam
 * @param productId 
   * @return ComplianceQueryResponse
   * @throws Exception
   */
  @Log
  public ComplianceQueryResponse complianceQuery(ComplianceQueryParam queryParam, Long productId) throws Exception {    
    try {
    	 int foreignCreditOpenFlag = getCreditChoice(productId);
    	 String queryUrl ="";
         if(foreignCreditOpenFlag==1){
        	 queryUrl = foreignQueryUrl;
         }else{
        	 queryUrl = assureQueryUrl;
         }
	      String paramData = JacksonUtil.Obj2Json(queryParam);
	      String result = HttpRequest.doJson(queryUrl, paramData, 30);
	      ComplianceQueryResponse response =
	          JacksonUtil.Json2Obj(result,
	              new TypeReference<ComplianceQueryResponse>() {});
	      if (!response.getIsSeccess()) {
	        throw new Exception("合规查询 失败");
	      }
	      return response;
    } catch (Exception e) {
      throw new BirdRequestException(e.getMessage());
    }
  }

  @Log
  public ReportStateResponse getState(String idNo, Long productId) throws Exception {
	    int foreignCreditOpenFlag = getCreditChoice(productId);
		String stateUrl ="";
	    if(foreignCreditOpenFlag==1){
	    	stateUrl = foreignStateUrl;
	    }else{
	    	stateUrl = assureStateUrl;
	    }  
	    ReportStateResponse response = new ReportStateResponse();   
	    BirdResponse birdResponse = super.getBirdData(stateUrl + idNo,new int[]{0,1,110}); 
	    response =
	        JacksonUtil.Json2Obj(birdResponse.getOriginalResult(),
	            new TypeReference<ReportStateResponse>() {});
	    // LOGGER.info("客户"+idNo+"征信状态查询结果："+JacksonUtil.Obj2Json(response));
	    return response;
  }

  @Log
  public ReportStateResponse getReport(String idNo, Long productId) throws Exception {
	int foreignCreditOpenFlag = getCreditChoice(productId);
	String resultUrl ="";
	if(foreignCreditOpenFlag==1){
		resultUrl = foreignResultUrl;
	}else{
		resultUrl = assureResultUrl;
	}    
    ReportStateResponse response = new ReportStateResponse();
    BirdResponse birdResponse = super.getBirdData(resultUrl + idNo,new int[]{0,1,110});
    response =
        JacksonUtil.Json2Obj(birdResponse.getOriginalResult(),
            new TypeReference<ReportStateResponse>() {});
    return response;
  }

  @Log
  public ReportStateResponse foreignSubmit(ForeignSubmitParam submitParam) throws Exception {
    String paramData = JacksonUtil.Obj2Json(submitParam);
    String result = HttpRequest.doJson(foreignSubmitUrl, paramData, 30);
   // BirdResponse birdResponse = super.postBirdData(foreignSubmitUrl, paramData,new int[]{0,1,110},30);
    ReportStateResponse response =
        JacksonUtil.Json2Obj(result,
            new TypeReference<ReportStateResponse>() {});
    return response;
  }
  
  @Log
  public AssureSubmitResponse assureSubmit(AssureSubmitParam submitParam) throws Exception {
    String paramData = JacksonUtil.Obj2Json(submitParam);
    String result = HttpRequest.doJson(assureSubmitUrl, paramData, 30);
   // BirdResponse birdResponse = super.postBirdData(foreignSubmitUrl, paramData,new int[]{0,1,110},30);
    AssureSubmitResponse response =
        JacksonUtil.Json2Obj(result,
            new TypeReference<AssureSubmitResponse>() {});
    return response;
  }
  @Log
  public PreAuditResponse preAudit(PreAuditParam param, String idNo) throws Exception {
	  String paramData = JacksonUtil.Obj2Json(param);
	  String result;
	  try {
		  result = HttpRequest.doJson(preAuditUrl+idNo, paramData, 30);
	  } catch (Exception e) {
		  throw new Exception("预审接口异常："+e.getMessage());
	  }
	  PreAuditResponse response = JacksonUtil.Json2Obj(result,
	            new TypeReference<PreAuditResponse>() {});
	  return response;
  }
  
  /**根据枚举值确定走担保还是外贸版征信，默认外贸
	 * 
	 * @param productId
	 * @return
	 */
	public int getCreditChoice(Long productId){
		if(productId !=null){						
			for(ProductBusTypeEnum e : ProductBusTypeEnum.values()){
				if(productId == e.getId()){
					return e.getCreditChoice();
				}
			}
		}
		return 1;
	}
}
